use dta/MunicipalityLevel.dta, clear
drop if SMD==. /* these are old knr 301 601 etc. that have been classified as 3001 etc */

gen urban=0
replace urban=1 if SMD>3000  /* ** oslo (5), drammen (2), kristiansand (2) stavanger (2), bergen (4), trondheim (4) */

foreach SMD in 190 191 192 193 490 500 690 790 791 792 890 891 892 893 990 991 1090 1190 1590 1591 1800 1900 2000 {
replace urban=1 if SMD==`SMD'
}

egen max_M=max(M), by(SMD)
replace max_M=7 if SMD==3001
replace max_M=7 if SMD==3002
replace max_M=7 if SMD==3003
replace max_M=7 if SMD==3004
replace max_M=7 if SMD==3005
replace max_M=3 if SMD==6001
replace max_M=3 if SMD==6002
replace max_M=7 if SMD==10001  /* kristiansand merged with stavanger etc */
replace max_M=7 if SMD==10002
replace max_M=7 if SMD==11001
replace max_M=7 if SMD==11002
replace max_M=5 if SMD==13001
replace max_M=5 if SMD==13002
replace max_M=5 if SMD==13003
replace max_M=5 if SMD==13004
replace max_M=5 if SMD==16001
replace max_M=5 if SMD==16002
replace max_M=5 if SMD==16003
replace max_M=5 if SMD==16004

****************************************************************
************** IDENTIFY SAMPLE 91 ******************************
****************************************************************

gen Sample91=1
replace Sample91=0 if knr>3000  /* Cannot measure at the SMD-level post-reform */
replace Sample91=0 if knr==301|knr==602|knr==1001|knr==1103|knr==1301|knr==1601|knr==1701 /* big cities post-reform*/

*** In the transition from SMD to PR a few districts are split, this reduce number of districts to 104
replace Sample91=0 if SMD==892 /* Brevik and Holmestrand (SMD_district 892) was put in two different PR_districts (25 and 26). */
replace Sample91=0 if SMD==1001 /* Mandal (SMD_district 1001, rural) was moved to an urban PR_district (27), while the other SMD_districts in 1001 went to PR district 10 (together with 1002, 1003 and 1004). */
replace Sample91=0 if SMD==690  /* Kongsberg, Hønefoss og Notodden (SMD_district 690) was put in two different PR_districts (24 and 25)*/

** From 1915 to 1918 the structure change same somewhat: 
replace Sample91=0 if SMD==500|SMD==490 /* Lillehammer, Hamar, Gjøvik and Kongsvinger split in two from 1915 to 1918 */
/* o	Extra district added in Romsdal, and then a few municipalities are moved around*/
replace Sample91=0 if SMD==1501
replace Sample91=0 if SMD==1502
replace Sample91=0 if SMD==1503
replace Sample91=0 if SMD==1504
replace Sample91=0 if SMD==1505
replace Sample91=0 if SMD==1506
/* o	Extra district added in Troms, and then a few municipalities are moved around */
replace Sample91=0 if SMD==1901
replace Sample91=0 if SMD==1902
replace Sample91=0 if SMD==1903
replace Sample91=0 if SMD==1904
/* 1280 Aarstad leaves SMD 1203 and becomes part of Bergen from 1915, onwards */
replace Sample91=0 if SMD==1203

****************************************************************
****************************************************************
****************************************************************
collapse urban max_M Sample91 (sum) votes1* (sum) votes2*, by(year SMD)

******************************************************************
******************************************************************

foreach round in 1 2 {
	foreach parti in s v_a v a l t kir h_fv h fv oth nsa nkp {
	gen vs`round'_`parti'=votes`round'_`parti'/votes`round'_TOTAL
	gen run`round'_`parti'=0
	replace run`round'_`parti'=1 if votes`round'_`parti'!=0
	}
}

order year SMD votes1* vs1* run1* votes2* vs2* run2*

******************************************************************
******************************************************************
egen NoP1=rowtotal(run1_s-run1_nkp)
egen NoP2=rowtotal(run2_s-run2_nkp)

gen SECOND=0
replace SECOND=1 if votes2_TOTAL!=0  

gen NoPF=NoP1
replace NoPF=NoP2 if SECOND==1

foreach party in s v_a v a l t kir h_fv h fv oth nsa nkp {
gen runF_`party'=run1_`party'
replace runF_`party'=run2_`party' if SECOND==1
}

**************** ENOP FIRST ROUND *********************************

foreach party in s v_a v a l t kir h_fv h fv oth nsa nkp {
gen vs1SQ_`party'=vs1_`party'*vs1_`party'
}
egen temp=rowtotal(vs1SQ_s-vs1SQ_nkp)
gen ENoP1=1/temp
drop temp

**************** ENOP FINAL ROUND *********************************

foreach party in s v_a v a l t kir h_fv h fv oth nsa nkp {
gen vsF_`party'=vs1_`party'
replace vsF_`party'=vs2_`party' if SECOND==1
}

foreach party in s v_a v a l t kir h_fv h fv oth nsa nkp {
gen vsFSQ_`party'=vsF_`party'*vsF_`party'
}
egen temp=rowtotal(vsFSQ_s-vsFSQ_nkp)
gen ENoPF=1/temp
drop temp *SQ*


*********************** CREATE BLOCS **********************************************
foreach round in 1 2 {
gen vs`round'_LAB=vs`round'_s+vs`round'_nsa+vs`round'_nkp 
gen vs`round'_LIB=vs`round'_a+vs`round'_v_a+vs`round'_v 
gen vs`round'_CON=vs`round'_fv+vs`round'_h_fv+vs`round'_h 
gen vs`round'_OTH=1-vs`round'_LAB-vs`round'_LIB-vs`round'_CON
}

foreach bloc in LAB LIB CON OTH {
gen vsF_`bloc'=vs1_`bloc'
replace vsF_`bloc'=vs2_`bloc' if SECOND==1
}

foreach bloc in LAB LIB CON OTH {
egen min_vs1_`bloc'=min(vs1_`bloc'), by(SMD)
}
gen Sample42=1
replace Sample42=0 if Sample91==0
replace Sample42=0 if min_vs1_LAB==0
replace Sample42=0 if min_vs1_LIB==0
replace Sample42=0 if min_vs1_CON==0

*** September 2018
replace Sample42=1 if SMD==1591   /* INCLUDE KRISTIANSUND WHERE H_FV IS RUNNING IN 1924, BUT DO NOT GET ANY VOTES */

***************
keep if year==1918
tab1 Sample*

sum urban vs1_LAB vs1_LIB vs1_CON vs1_OTH ENoP1 SECOND max_M
sum urban vs1_LAB vs1_LIB vs1_CON vs1_OTH ENoP1 SECOND max_M if Sample91==1
sum urban vs1_LAB vs1_LIB vs1_CON vs1_OTH ENoP1 SECOND max_M if Sample42==1


eststo clear
estpost sum urban vs1_LAB vs1_LIB vs1_CON vs1_OTH ENoP1 SECOND max_M
eststo a
estpost sum urban vs1_LAB vs1_LIB vs1_CON vs1_OTH ENoP1 SECOND max_M if Sample91==1
eststo b
estpost sum urban vs1_LAB vs1_LIB vs1_CON vs1_OTH ENoP1 SECOND max_M if Sample42==1
eststo c
esttab a b c using tables/TableA1.tex, title(Description of estimation samples using data from the 1918 election /label{Sample}) style(tex) ///
cells("mean(fmt(2)) sd(par)") stats(N)  mtitles("All" "Sub-set 1" "Sub-set 2") replace ///
coeflabels(urban "Urban district" vs1_LAB "Voteshare LAB" vs1_LIB "Voteshare LIB" vs1_CON "Voteshare CON" vs1_OTH "Voteshare OTH" SECOND "Second round" max_M "Post-reform magnitude")  
